package handlers

import (
	"context"
	"ourplansystem/server/ent"
	"ourplansystem/server/models"
	"strconv"
	"time"

	"github.com/gofiber/fiber/v2"
)

// AnniversaryHandler 纪念日处理器
type AnniversaryHandler struct {
	client *ent.Client
}

// NewAnniversaryHandler 创建纪念日处理器
func NewAnniversaryHandler(client *ent.Client) *AnniversaryHandler {
	return &AnniversaryHandler{client: client}
}

// CreateAnniversaryRequest 创建纪念日请求
type CreateAnniversaryRequest struct {
	Title       string `json:"title"`
	Date        string `json:"date"` // 格式: 2006-01-02
	Type        string `json:"type"`
	Description string `json:"description"`
}

// UpdateAnniversaryRequest 更新纪念日请求
type UpdateAnniversaryRequest struct {
	Title       string `json:"title"`
	Date        string `json:"date"`
	Type        string `json:"type"`
	Description string `json:"description"`
}

// GetAnniversaries 获取所有纪念日
func (h *AnniversaryHandler) GetAnniversaries(c *fiber.Ctx) error {
	ctx := context.Background()
	anniversaries, err := h.client.Anniversary.Query().All(ctx)
	if err != nil {
		return models.InternalServerError(c, "获取纪念日列表失败")
	}

	return models.Success(c, anniversaries)
}

// GetAnniversary 获取单个纪念日
func (h *AnniversaryHandler) GetAnniversary(c *fiber.Ctx) error {
	ctx := context.Background()
	id, err := strconv.ParseInt(c.Params("id"), 10, 64)
	if err != nil {
		return models.BadRequest(c, "无效的纪念日ID")
	}

	anniversary, err := h.client.Anniversary.Get(ctx, id)
	if err != nil {
		if ent.IsNotFound(err) {
			return models.NotFound(c, "纪念日不存在")
		}
		return models.InternalServerError(c, "获取纪念日失败")
	}

	return models.Success(c, anniversary)
}

// CreateAnniversary 创建纪念日
func (h *AnniversaryHandler) CreateAnniversary(c *fiber.Ctx) error {
	ctx := context.Background()
	var req CreateAnniversaryRequest
	if err := c.BodyParser(&req); err != nil {
		return models.BadRequest(c, "请求参数错误")
	}

	if req.Title == "" {
		return models.BadRequest(c, "标题不能为空")
	}

	// 解析日期
	date, err := time.Parse("2006-01-02", req.Date)
	if err != nil {
		return models.BadRequest(c, "日期格式错误，应为: 2006-01-02")
	}

	builder := h.client.Anniversary.
		Create().
		SetTitle(req.Title).
		SetDate(date)

	if req.Type != "" {
		builder.SetType(req.Type)
	}

	if req.Description != "" {
		builder.SetDescription(req.Description)
	}

	anniversary, err := builder.Save(ctx)
	if err != nil {
		return models.InternalServerError(c, "创建纪念日失败")
	}

	return models.SuccessWithMessage(c, "创建成功", anniversary)
}

// UpdateAnniversary 更新纪念日
func (h *AnniversaryHandler) UpdateAnniversary(c *fiber.Ctx) error {
	ctx := context.Background()
	id, err := strconv.ParseInt(c.Params("id"), 10, 64)
	if err != nil {
		return models.BadRequest(c, "无效的纪念日ID")
	}

	var req UpdateAnniversaryRequest
	if err := c.BodyParser(&req); err != nil {
		return models.BadRequest(c, "请求参数错误")
	}

	builder := h.client.Anniversary.UpdateOneID(id)

	if req.Title != "" {
		builder.SetTitle(req.Title)
	}

	if req.Date != "" {
		date, err := time.Parse("2006-01-02", req.Date)
		if err != nil {
			return models.BadRequest(c, "日期格式错误")
		}
		builder.SetDate(date)
	}

	if req.Type != "" {
		builder.SetType(req.Type)
	}

	if req.Description != "" {
		builder.SetDescription(req.Description)
	}

	anniversary, err := builder.Save(ctx)
	if err != nil {
		if ent.IsNotFound(err) {
			return models.NotFound(c, "纪念日不存在")
		}
		return models.InternalServerError(c, "更新纪念日失败")
	}

	return models.SuccessWithMessage(c, "更新成功", anniversary)
}

// DeleteAnniversary 删除纪念日
func (h *AnniversaryHandler) DeleteAnniversary(c *fiber.Ctx) error {
	ctx := context.Background()
	id, err := strconv.ParseInt(c.Params("id"), 10, 64)
	if err != nil {
		return models.BadRequest(c, "无效的纪念日ID")
	}

	err = h.client.Anniversary.DeleteOneID(id).Exec(ctx)
	if err != nil {
		if ent.IsNotFound(err) {
			return models.NotFound(c, "纪念日不存在")
		}
		return models.InternalServerError(c, "删除纪念日失败")
	}

	return models.SuccessWithMessage(c, "删除成功", nil)
}
